
package org.buildforce.tomcat.mojos.deployment;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.buildforce.tomcat.TomcatConfigurationException;
import org.buildforce.tomcat.TomcatManager;
import org.buildforce.tomcat.util.FileSystemUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author  Martin Todorov
 */
public abstract class AbstractArtifactDeploymentMojo extends CopyWebResourcesMojo
{

    public List includesList = new ArrayList();
    public List excludesList = new ArrayList();

    /**
     * @parameter default-value="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * @parameter default-value="${project.build.directory}" required="false"
     */
    private String projectBuildOutputDirectory;


    public void execute()
            throws MojoExecutionException, MojoFailureException
    {
        Resource resource = prepareResources();

        TomcatManager manager = new TomcatManager(getTomcatConfiguration(), getLog());
        manager.stopTomcat();

        // Copy the default web resources (could be the jars or war artifacts):
        FileSystemUtils.copyResources(new Resource[]{ resource }, getLog());

        // Copy the additional resources (if such are defined):
        if (getResources() != null)
            FileSystemUtils.copyResources(getResources(), getLog());

        manager.startTomcat();
    }

    public abstract Resource prepareResources() throws TomcatConfigurationException;
    
    public MavenProject getProject()
    {
        return project;
    }

    public String getProjectBuildOutputDirectory()
    {
        return projectBuildOutputDirectory;
    }

    public void setProjectBuildOutputDirectory(String projectBuildOutputDirectory)
    {
        this.projectBuildOutputDirectory = projectBuildOutputDirectory;
    }

}
